package com.gzx.plugin.fin.tech.utils;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Random;
import java.util.Set;

/**
 * @author zjt
 * @description
 * @date 2025/5/12 17:26
 */
public class ScoreUtil {

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMM");

    private static final Random RANDOM = new Random();

    /**
     * 离散时效评分
     *
     * @param targetDateStr
     * @param dateSet
     * @return
     */
    public static int discreteScore(String targetDateStr, Set<String> dateSet) {
        // 如果未找到记录，返回0
        if (CollectionUtil.isEmpty(dateSet)) {
            return 0;
        }
        int minScore = Integer.MAX_VALUE;
        YearMonth targetDate;
        try {
            targetDate = YearMonth.now();
            if (StringUtils.isNotBlank(targetDateStr)) {
                targetDate = YearMonth.from(LocalDate.parse(targetDateStr));
            }
        } catch (Exception e) {
            // 如果日期解析失败，使用当前日期
            e.printStackTrace();
            targetDate = YearMonth.now();
        }
        for (String dateStr : dateSet) {
            try {
                YearMonth recordDate = YearMonth.parse(dateStr, DATE_FORMATTER);
                long monthsBetween = ChronoUnit.MONTHS.between(recordDate, targetDate);
                if (monthsBetween >= 0) {
                    int currentScore;
                    if (monthsBetween <= 6) {
                        // 6个月内来案 - 最高优先级(分数1)
                        currentScore = 1;
                    } else {
                        // 超过6个月 - 次优先级(分数2)
                        currentScore = 2;
                    }
                    // 更新最小分数
                    if (currentScore < minScore) {
                        minScore = currentScore;
                        // 如果已经找到最高优先级(1)，可以直接返回
                        if (minScore == 1) {
                            return 1;
                        }
                    }
                }
            } catch (Exception e) {
                // 日期格式错误等情况，跳过这个日期
                e.printStackTrace();
            }
        }
        // 如果没有符合条件的日期(monthsBetween > 0)，返回0
        return minScore != Integer.MAX_VALUE ? minScore : 0;
    }

    /**
     * 连续时效评分
     *
     * @param targetDateStr
     * @param dateSet
     * @return
     */
    public static int continuousScore(String targetDateStr, Set<String> dateSet) {
        if (dateSet == null || dateSet.isEmpty()) {
            return RANDOM.nextInt(60);
        }
        int maxScore = 0;
        for (String dateStr : dateSet) {
            try {
                YearMonth recordDate = YearMonth.parse(dateStr, DATE_FORMATTER);
                YearMonth targetDate = YearMonth.now();
                if (StringUtils.isNotBlank(targetDateStr)) {
                    targetDate = YearMonth.parse(targetDateStr, DATE_FORMATTER);
                }
                long monthsBetween = ChronoUnit.MONTHS.between(recordDate, targetDate);
                int currentScore;
                if (monthsBetween <= 1) {
                    currentScore = 95 + RANDOM.nextInt(6); // 95-100
                } else if (monthsBetween <= 3) {
                    currentScore = 90 + RANDOM.nextInt(5); // 90-94
                } else if (monthsBetween <= 6) {
                    currentScore = 85 + RANDOM.nextInt(5); // 85-89
                } else if (monthsBetween <= 12) {
                    currentScore = 80 + RANDOM.nextInt(5); // 80-84
                } else {
                    currentScore = RANDOM.nextInt(80); // 0-79
                }
                // 更新最大分数
                if (currentScore > maxScore) {
                    maxScore = currentScore;
                }
            } catch (Exception e) {
                // 日期格式错误等情况，不影响其他日期的计算
                e.printStackTrace();
            }
        }
        return maxScore;
    }
}
